import 'dart:async';
import 'dart:typed_data';
import 'package:my_shop/util/share_info.dart';
import 'package:fluwx/fluwx.dart';
// import 'package:tencent_kit/tencent_kit.dart';
// import 'package:weibo_kit/weibo_kit.dart';
import 'package:http/http.dart' as http;

class ShareUtil{

  /*
  * 微信、朋友圈分享
  * 引入库：fluwx: ^3.13.1
  * */
  shareWChat(ShareOutInfo shareOutInfo, WeChatScene scene, Function shareBack) async {
    Fluwx fluwx = Fluwx();
    await fluwx.registerApi(appId: "wxd930ea5d5a228f5f",universalLink: "https://your.univerallink.com/link/");
    var result = await fluwx.isWeChatInstalled;
    if (result == false) {
      print('请先安装微信');
      return;
    }
    // 微信分享
    bool _isShared = await fluwx.share(WeChatShareWebPageModel(
        shareOutInfo.shareUrl,
        thumbnail: WeChatImage.network(shareOutInfo.thumbnail),
        scene: scene,
        //   scene: WeChatScene.SESSION,//会话
        //   scene: WeChatScene.TIMELINE,//朋友圈
        //   scene: WeChatScene.FAVORITE,//收藏
        title: shareOutInfo.title,
        description: shareOutInfo.subTitle));
    if(shareBack != null){
      if(_isShared == true){
        shareBack(true);
      }else{
        shareBack(false);
      }
    }
  }

  /*
  * QQ分享
  * sharePosition 0：QQ分享， 否则QQ空间分享
  * 引入库：tencent_kit: ^3.1.0
  * */
  // shareQQ(ShareOutInfo shareOutInfo, int sharePosition) async {
  //   await Tencent.instance.registerApp(appId: 'QQ appid');
  //   Tencent.instance.setIsPermissionGranted(granted: true);
  //   if (!await Tencent.instance.isQQInstalled()) {
  //     print('清先安装QQ');
  //     return;
  //   }
  //   await Tencent.instance.shareWebpage(
  //       scene: sharePosition == 0 ? TencentScene.SCENE_QQ : TencentScene.SCENE_QZONE,
  //       //  scene: TencentScene.SCENE_QQ, //QQ会话
  //       //  scene: TencentScene.SCENE_QZONE, //QQ空间
  //       title: shareOutInfo.title,
  //       summary: shareOutInfo.subTitle,
  //       imageUri: Uri.parse(shareOutInfo.thumbnail),
  //       targetUrl: shareOutInfo.shareUrl);
  // }

  /*
  * 微博分享
  * 引入库：weibo_kit: 3.1.0
  * 这个库限制图片大小不能大于1024 * 1024 * 0.5
  * */
  // shareWeibo(ShareOutInfo shareOutInfo) async {
  //   await Weibo.instance.registerApp(
  //       appKey: '',
  //       universalLink: '',
  //       scope: [WeiboScope.ALL]);
  //
  //   bool _isInstalled = await Weibo.instance.isInstalled();
  //   if(!_isInstalled){
  //     print('您可能未安装微博');
  //     return;
  //   }
  //   // Loading.showLoadingText(navigatorKey.currentContext,title: '处理中...');
  //   Uint8List imageData =  await getImageBytesFromUrl(shareOutInfo.thumbnail,true);
  //   // Loading.hideLoading(navigatorKey.currentContext);
  //   if(imageData!=null){
  //     await Weibo.instance.shareWebpage(
  //         title: shareOutInfo.title,
  //         description: shareOutInfo.subTitle,
  //         thumbData: imageData.buffer.asUint8List(),
  //         webpageUrl: shareOutInfo.shareUrl
  //     );
  //   }
  // }


  /*
  * 从图片网络地址获取Uint8List数据
  * 引入库：http: 0.13.5
  * */
  Future<Uint8List> getImageBytesFromUrl(String url,bool toCompress) async {
    Completer<Uint8List> _completer = new Completer<Uint8List>();
    final response = await http.get(Uri.parse(url));
    if (response.statusCode == 200) {
      Uint8List _thumbData = Uint8List.fromList(response.bodyBytes);
      print('response.data= $_thumbData');
      /*if(toCompress){
        if (_thumbData.length > 1024 * 1024 * 0.5) {
          final image.Image thumbnail = image.decodeImage(_thumbData);
          int _quality = 90;
          print("图片初始大小：${_thumbData.length/ 1024 /1024}MB");
          while(_thumbData.length > 1024 * 1024 * 0.5 && _quality > 0){
            _thumbData = Uint8List.fromList(image.encodeJpg(thumbnail,quality: _quality));
            _quality -= 4;
          }
          print("图片压缩后大小：${_thumbData.length} ${_thumbData.length/ 1024 / 1024}MB");
        }
      }*/
      _completer.complete(_thumbData);
    } else {
      _completer.complete(null);
    }
    return _completer.future;
  }

  /*
  * 钉钉分享
  * 引入库：flu_dingtalk: 1.0.0+3
  * */
/*  shareDingTalk(ShareOutInfo shareOutInfo,Function shareBack) async {
    //注册钉钉插件
    // FlutterDingtalk.registerApp('钉钉appkey','com.test.pro');
    // FlutterDingtalk.registerApp('','com.youhengmedia.atui');
    try{
      bool _isSupport = await FluDingtalk.registerApp("钉钉appkey(ding开头的一串字符串)", "com.test.pro");
      if(_isSupport){
        bool isInstalled = await FluDingtalk.isDingDingInstalled();
        if(!isInstalled){
          esLoadingToast('您可能未安装钉钉');
          return;
        }
        bool _shared =  await FluDingtalk.sendWebPageMessage(
          shareOutInfo.shareUrl,
          title: shareOutInfo.title,
          content: shareOutInfo.subTitle,
          thumbUrl: shareOutInfo.thumbnail,
        );
        if(shareBack != null){
          shareBack(_shared);
        }
      }
    }catch(e){}
  }*/
}


